package com.lanou.controller;

import com.lanou.bean.Cost;
import com.lanou.service.CostService;
import com.lanou.utils.AjaxResult;
import com.lanou.utils.GetDate;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by dllo on 18/3/15.
 */
@Controller
public class CostController {

    @Resource
    CostService costService;

    @RequestMapping(value = "/fee_list")
    public String fee_list() {
        if (SecurityUtils.getSubject().isPermitted("资费管理")) {
            return "fee/fee_list";
        } else {
            return "nopower";
        }
    }

    @RequestMapping(value = "/fee_add")
    public String fee_add() {
        return "fee/fee_add";
    }

    @RequestMapping(value = "/fee_modi")
    public String fee_modi() {
        return "fee/fee_modi";
    }

    @RequestMapping(value = "/fee_detail")
    public String fee_detail() {
        return "fee/fee_detail";
    }

    /**
     * 查询所有资费信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllCost")
    public List<Cost> selectAllCost() {

        return costService.selectAllCost();
    }

    /**
     * 查询所有资费信息按基本费用排序
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllCost1")
    public List<Cost> selectAllCost1() {
        return costService.selectAllCost1();
    }

    /**
     * 查询所有资费信息按基本时长排序
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllCost2")
    public List<Cost> selectAllCost2() {
        return costService.selectAllCost2();
    }

    /**
     * 资费列表设置暂停启用状态
     * @param costId
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/startOrStop")
    public AjaxResult startOrStop(@RequestParam("costId") int costId, @RequestParam("status") String status) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/fee_list");
        if (status.equals("0")) {
            status = "1";
        } else {
            return ajaxResult;
        }


        costService.startOrStop(costId, status, GetDate.getTime());

        return ajaxResult;
    }

    /**
     * 根据资费Id删除资费信息
     * @param costId
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delCost")
    public AjaxResult delCost(@RequestParam("costId") int costId,@RequestParam("status") String status) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/fee_list");
        if (status.equals("0")) {
            costService.deleCost(costId);
            return ajaxResult;
        } else {
            return ajaxResult;
        }

    }

    /**
     * 根据资费Id查询资费信息
     * @param costId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectCostById")
    public Cost selectCostById(@RequestParam("costId") int costId) {
        Cost cost = costService.selectCostById(costId);
        return cost;
    }

    /**
     * 根据资费id修改资费信息
     * @param costId
     * @param name
     * @param baseDuration
     * @param baseCost
     * @param unitCost
     * @param descr
     * @param costType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/modifyCost")
    public AjaxResult modifyCost(@RequestParam("costId") int costId,
                                 @RequestParam("name") String name,
                                 @RequestParam("baseDuration") int baseDuration,
                                 @RequestParam("baseCost") int baseCost,
                                 @RequestParam("unitCost") int unitCost,
                                 @RequestParam("descr") String descr,
                                 @RequestParam("costType") String costType) {

        costService.updataCost(costId,name,baseDuration,baseCost,unitCost,descr,costType);

        AjaxResult result = new AjaxResult();
        result.setMsg("/fee_list");
        return result;
    }

    /**
     * 添加资费信息
     * @param name
     * @param costType
     * @param baseDuration
     * @param baseCost
     * @param unitCost
     * @param descr
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addcost")
    public AjaxResult addCost(@RequestParam("name")String name,
                        @RequestParam("costType")String costType,
                        @RequestParam(value = "baseDuration") int baseDuration,
                        @RequestParam(value = "baseCost") int baseCost,
                        @RequestParam(value = "unitCost") int unitCost,
                        @RequestParam("descr") String descr){

        Cost cost = new Cost();
        cost.setName(name);
        cost.setBaseDuration(baseDuration);
        cost.setBaseCost(baseCost);
        cost.setUnitCost(unitCost);
        cost.setCostType(costType);
        cost.setDescr(descr);
        cost.setStatus("0");
        cost.setCreatime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(System.currentTimeMillis())));
        costService.addCost(cost);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/fee_list");
        return ajaxResult;
    }
}
